package com.hx.dc.controllor;

import com.hx.dc.entity.CommoDity;
import com.hx.dc.entity.MenuPlan;
import com.hx.dc.service.SeeMenuService;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Lenovo on 2019/2/15.
 */

@Controller
@RequestMapping("seemenu")
public class MenuPlanAction extends BaseAction{

    @Autowired
    private SeeMenuService seeMenuService;

/*    查询菜单*/
@RequestMapping("seemen")
@ResponseBody
    public  Object SeeMenu(String id){
        MenuPlan mneu=new MenuPlan();

        if(id==null){
            mneu.setMenuFather("0");
            List <MenuPlan> sx = seeMenuService.SeeMenu(mneu);
             List<Map> e= new ArrayList<>();
            for (MenuPlan m:sx){
                Map map = new HashMap();
                map.put("id",m.getMenuId());
                map.put("text",m.getMenuType());
                map.put("state","closed");
                e.add(map);
            }
            return e;
        }else {
          mneu.setMenuFather("1");
          List<MenuPlan> me=seeMenuService.SeeMenu(mneu);
          List<Map> e=new ArrayList<>();
          for (MenuPlan m :me){
              Map map = new HashMap();
              map.put("id",m.getMenuId());
              map.put("text",m.getMenuType());
              map.put("state","open");
              e.add(map);
          }
        return e;
        }
    }

   /* 初始化菜单*/
    @RequestMapping("seem")
    @ResponseBody
    public Object selectPage(MenuPlan menuPlan, Integer page, Integer rows) {
        System.out.println("page:" + page + ",rows:" + rows);

        return this.getPage(seeMenuService.selectPage(menuPlan, page, rows));
    }

    /*查询数据*/
    @RequestMapping("seeh")
    @ResponseBody
    public Object SeeMenuPlan(String id){
        System.out.print("1111111111111111111111111111111111id=="+id);
        return seeMenuService.SeeMenuPlan(id );
    }


   /* 清除所有*/
   @RequestMapping("delete")
   @ResponseBody
    public Object DeleteMenuPlan(String id){

       return seeMenuService.DeleteMenuPlan( id);}

    /*添加所有*/
    @RequestMapping("insert")
    @ResponseBody
    public  Object InsertMenuPlan(String aaa){
          System.out.print("11111111111111111111111111="+aaa);

  try{
       MenuPlan m[] = null;
      ObjectMapper objectMapper= new ObjectMapper();
       m = objectMapper.readValue(aaa,MenuPlan[].class);
       List<MenuPlan> list =new ArrayList<>();
       for(int i=0;i<m.length;i++) {
           System.out.println("测试数据：" + m[i]);
       }
        list =Arrays.asList(m);
       String s;
       s= m[0].getMenuFather();
      seeMenuService.DeleteMenuPlan(s);

      return    seeMenuService.IsertMenuPlan(list);
   }catch (Exception e){
       e.printStackTrace();
   }
        return  null;
    }

   /* 删除小菜单*/
    @RequestMapping("deleteNote")
    @ResponseBody
    public  Object deleteNote(String id){

            System.out.print("111111111111111111111111====="+id);
        return  seeMenuService.deleteNote(id);
    }

       /* 修改小菜单*/
    @RequestMapping("ademnds")
    @ResponseBody
    public Object AdemendsMenu(String id,String name){

       MenuPlan menuPlan =new MenuPlan();
        menuPlan.setMenuFather(id);
        menuPlan.setMenuType(name);
        return seeMenuService.AdemendsMenu(menuPlan);
    }

/*   添加小菜单*/
    @RequestMapping("addMenuPlan")
    @ResponseBody
    public Object AddMenuplan(String  name){
        return seeMenuService.AddMenuPlan(name);
    };

}
